//
//  SeekoTabBarController.m
//  Moko
//
//  Created by cj848 on 11. 7. 18..
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "SeekoTabBarController.h"
#import "HTMLNode.h"
#import "MokoAppDelegate.h"
#import "ConfigurationTableViewController.h"
#import "SeekoBoardTableViewController.h"
#import "SeekoBoardNavigationController.h"
#import "MemoRootViewController.h"

@implementation SeekoTabBarController
@synthesize depth1_menus, depth2_menus, depth1_menus_text, depth2_menus_text;
-(void)viewDidLoad {
    [super viewDidLoad];
    self.moreNavigationController.navigationBar.tintColor = RGB(20, 20, 20);
    self.moreNavigationController.delegate = self;
    if( [self.moreNavigationController.topViewController.view isKindOfClass:[UITableView class]] == YES ) {
        UIViewController* moreViewController = [self.moreNavigationController.viewControllers objectAtIndex:0];
        moreViewController.title = @"설정/쪽지관리";
        moreViewController.tabBarItem.title = @"설정/쪽지관리";
        UISwipeGestureRecognizer* swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self.navigationController action:@selector(backStep)];
        swipe.direction = UISwipeGestureRecognizerDirectionRight;
        swipe.numberOfTouchesRequired = 1;
        [self.moreNavigationController.view addGestureRecognizer:swipe];
        [swipe release];
    }
}
-(void) dealloc {
    [depth2_menus_text release];
    [depth1_menus release];
    [depth2_menus release];
    [depth1_menus_text release];
    [super dealloc];
}
-(id) initWithMenus:(NSArray*)menus {
    self = [super init];
    if( self ) {
        NSMutableArray* controllers = [[NSMutableArray alloc] init];
        for( int i = 0, l = [menus count]; i < l; i++ ) {
            // 테이블 뷰 추가. 
            MenuVO* vo = [menus objectAtIndex:i];
            SeekoBoardTableViewController* tableBoard = [[SeekoBoardTableViewController alloc] initWithNibName:@"SeekoBoardTableViewController" bundle:[NSBundle mainBundle] menuVO:vo];
            
            NSString* filename = [NSString stringWithFormat:@"tabbar_%d.png", i];
            UITabBarItem* item = [[UITabBarItem alloc] initWithTitle:tableBoard.title image:[UIImage imageNamed:filename] tag:i+1];
            tableBoard.tabBarItem = item;
            [item release];
            SeekoBoardNavigationController* navi = [[SeekoBoardNavigationController alloc] initWithRootViewController:tableBoard];
            navi.delegate = self;
            [tableBoard release];
            [controllers addObject:navi];
            [navi release];

        }
        // 설정 테이블 뷰 탭바에 추가
        GET_APPDELEGATE;
        SeekoBoardNavigationController* configNavi = [[SeekoBoardNavigationController alloc] initWithRootViewController:delegate.configTableController];
        configNavi.delegate = self;
        UITabBarItem* item = [[UITabBarItem alloc] initWithTitle:@"설정/정보" image:[UIImage imageNamed:@"tabbar_4.png"] tag:5];
        delegate.configTableController.tabBarItem = item;
        
        [item release];
        [controllers addObject:configNavi];
        [configNavi release];
        
        // 메모 네비게이션 추가
        MemoRootViewController* memoController = [[[MemoRootViewController alloc] init] autorelease];
        SeekoBoardNavigationController* memoNavi = [[SeekoBoardNavigationController alloc] initWithRootViewController:memoController];
        memoNavi.delegate = self;
        item = [[UITabBarItem alloc] initWithTitle:@"쪽지관리" image:[UIImage imageNamed:@"tabbar_5.png"] tag:6];
        memoNavi.tabBarItem = item;
        [item release];
        [controllers addObject:memoNavi];
        [memoNavi release];
        
        // 메모 알림 노티 등록
        [[NSNotificationCenter defaultCenter] addObserver:memoController selector:@selector(memoNotification:) name:MEMO_NOTIFICATION object:nil];

        self.viewControllers = controllers;
        [controllers release];
    }
    return self;
}
-(void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    [viewController viewWillAppear:animated];
    // moreNavigationController의 편집 버튼 제거. 
    if( [navigationController isEqual:self.moreNavigationController] == YES ) {
        navigationController.navigationBar.topItem.rightBarButtonItem = nil;
    }
}
@end
